﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using Telerik.Web.Mvc;
using IndoReputation.Views.ViewModels;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Models.BusinessObject.Collections;
using IndoReputation.Commons;
using IndoReputation.Models.Constants;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class NewsController : BaseController
    {
        //
        // GET: /News/

        public ActionResult Index()
        {
            ViewData["ajax"] = true;
            ViewData["scrolling"] = true;
            ViewData["paging"] = true;
            ViewData["filtering"] = true;
            ViewData["grouping"] = true;
            ViewData["sorting"] = true;
            ViewData["showFooter"] = true;
            return View();
        }

        [GridAction]
        public ActionResult _Index()
        {
            return View(new GridModel(NewsBOL.GetAllNewsJoin()));
        }

        public void GetAllNewsCategory(int? parentId, NewsCategory parentNode, ref List<NewsCategory> afterBind, List<NewsCategory> source)
        {
            var filteredItems = source.Where(item => item.ParentID == parentId);
            NewsCategory n;
            foreach (var i in filteredItems)
            {
                NewsCategory _temp = new NewsCategory
                {
                    ChildLocations = i.ChildLocations,
                    CreateAt = i.CreateAt,
                    CreateBy = i.CreateBy,
                    Description = i.Description,
                    ID = i.ID,
                    Name = i.Name,
                    ParentID = i.ParentID,
                    UpdateAt = i.UpdateAt,
                    UpdateBy = i.UpdateBy
                };
                if (parentNode == null)
                {
                    afterBind.Add(_temp);
                }
                else
                {
                    parentNode.ChildLocations.Add(_temp);
                }
                n = _temp;
                GetAllNewsCategory(i.ID, n, ref afterBind, source);
            }
        }

        [HttpGet]
        public ActionResult Add()
        {
            // for DatePublished
            ViewData["selectedDate"] = DateTime.Now;
            // for MediaID
            var media = MediaBOL.GetAllMedia();
            // for news category
            var parent = NewsCategoryBOL.GetAllNewsCategory();
            List<NewsCategory> listNC = new List<NewsCategory>();
            GetAllNewsCategory(null, null, ref listNC, parent);
            //
            var parameter = NewsParameterBOL.GetAllNewsParameter();
            List<NewsValuesModel> newsParameterList = new List<NewsValuesModel>();
            foreach (NewsParameter _n in parameter)
            {
                newsParameterList.Add(new NewsValuesModel
                {                    
                    NewsParameterID = _n.ID,
                    NewsParameterName = _n.Name,
                    NewsParameterValueList = CommonFunctions.ToSelectList<IRConstants.ChooseNews>(new IRConstants.ChooseNews()),
                    NewsMarkList = CommonFunctions.ToSelectList<IRConstants.NewsMark>(new IRConstants.NewsMark())
                });
            }
            var model = new NewsViewModel
            {                
                Media = new SelectList(media, "ID", "Name"),
                ListNewsCategory = listNC,
                ListNewsValuesModel = newsParameterList
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Add(NewsViewModel model)
        {
            if (ModelState.IsValid)
            {
                NewsCategory newsCategory = NewsCategoryBOL.GetNewsCategoryById(model.NewsCategory);
                News news = new News();
                news.NewsCategory = model.NewsCategory;
                news.NewsCategoryName = newsCategory.Name;
                news.MediaID = model.MediaID;
                news.MediaName = model.MediaName;
                news.DatePublished = model.DatePublished;
                news.Title = model.Title;
                news.Sumary = model.Sumary;
                news.Link = model.Link;
                string btnSubmit = Request["btnSubmit"];
                switch (btnSubmit)
                {
                    case "Save":                        
                        news.IsPublished = false;
                        break;
                    case "Save & Published":                        
                        news.IsPublished = true;
                        break;
                }
                Int64 newsID = NewsBOL.Insert(news);
                // Add Value
                if (newsID > 0)
                {
                    foreach (NewsValuesModel _n in model.ListNewsValuesModel)
                    {
                        NewsValues nVal = new NewsValues();
                        nVal.NewsID = newsID;
                        nVal.NewsParameterID = _n.NewsParameterID;
                        IRConstants.ChooseNews x = CommonFunctions.StringToEnum<IRConstants.ChooseNews>(_n.NewsParameterValue);
                        switch (x)
                        {
                            case IRConstants.ChooseNews.No:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.No;
                                break;
                            case IRConstants.ChooseNews.NotSure:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.NotSure;
                                break;
                            case IRConstants.ChooseNews.Yes:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.Yes;
                                break;
                            default:
                                break;
                        }
                        IRConstants.NewsMark newsMark = CommonFunctions.StringToEnum<IRConstants.NewsMark>(_n.NewsMarkString);
                        switch (newsMark)
                        {
                            case IRConstants.NewsMark.one:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.one;
                                break;
                            case IRConstants.NewsMark.two:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.two;
                                break;
                            case IRConstants.NewsMark.three:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.three;
                                break;
                            case IRConstants.NewsMark.four:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.four;
                                break;
                            case IRConstants.NewsMark.five:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.five;
                                break;
                            case IRConstants.NewsMark.six:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.six;
                                break;
                            case IRConstants.NewsMark.seven:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.seven;
                                break;
                            case IRConstants.NewsMark.eight:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.eight;
                                break;
                            case IRConstants.NewsMark.nine:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.nine;
                                break;
                            case IRConstants.NewsMark.ten:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.ten;
                                break;
                            default:
                                break;
                        }
                        NewsValuesBOL.Insert(nVal);                 
                    }                
                }                 
            }
            else
            {
                return View(model);
            }
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Edit(int newsID)
        {
            News news = NewsBOL.GetNewsById(newsID);            
            // for DatePublished
            ViewData["selectedDate"] = news.DatePublished;
            // for MediaID
            var media = MediaBOL.GetAllMedia();
            // for news category
            var parent = NewsCategoryBOL.GetAllNewsCategory();
            List<NewsCategory> listNC = new List<NewsCategory>();
            GetAllNewsCategory(null, null, ref listNC, parent);
            List<NewsValuesJoin> listValues = NewsValuesBOL.GetAllNewsValuesJoinByNewsID(newsID);           
            List<NewsValuesModel> newsParameterList = new List<NewsValuesModel>();
            foreach (NewsValuesJoin _n in listValues)
            {                
                NewsValuesModel newValmodel = new NewsValuesModel();
                newValmodel.CreateAt = _n.CreateAt;
                newValmodel.CreateBy = _n.CreateBy;
                newValmodel.ID = _n.ID;
                newValmodel.NewsID = _n.NewsID;                
                newValmodel.NewsMarkList = CommonFunctions.ToSelectList<IRConstants.NewsMark>(new IRConstants.NewsMark());
                switch (Convert.ToInt32(_n.NewsMark))
                {
                    case (int)IRConstants.NewsMark.one:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.one.ToString();
                        break;
                    case (int)IRConstants.NewsMark.two:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.two.ToString();
                        break;
                    case (int)IRConstants.NewsMark.three:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.three.ToString();
                        break;
                    case (int)IRConstants.NewsMark.four:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.four.ToString();
                        break;
                    case (int)IRConstants.NewsMark.five:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.five.ToString();
                        break;
                    case (int)IRConstants.NewsMark.six:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.six.ToString();
                        break;
                    case (int)IRConstants.NewsMark.seven:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.seven.ToString();
                        break;
                    case (int)IRConstants.NewsMark.eight:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.eight.ToString();
                        break;
                    case (int)IRConstants.NewsMark.nine:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.nine.ToString();
                        break;
                    case (int)IRConstants.NewsMark.ten:
                        newValmodel.NewsMarkString = IRConstants.NewsMark.ten.ToString();
                        break;
                    default:
                        break;
                }
                newValmodel.NewsParameterID = _n.NewsParameterID;
                newValmodel.NewsParameterName = _n.NewsParameterName;
                newValmodel.NewsParameterValueList = CommonFunctions.ToSelectList<IRConstants.ChooseNews>(new IRConstants.ChooseNews());
                switch (Convert.ToInt32(_n.NewsParameterValue))
                {
                    case (int)IRConstants.ChooseNews.No:
                        newValmodel.NewsParameterValue = IRConstants.ChooseNews.No.ToString();
                        break;
                    case (int)IRConstants.ChooseNews.NotSure:
                        newValmodel.NewsParameterValue = IRConstants.ChooseNews.NotSure.ToString();
                        break;
                    case (int)IRConstants.ChooseNews.Yes:
                        newValmodel.NewsParameterValue = IRConstants.ChooseNews.Yes.ToString();
                        break;
                    default:
                        break;
                }
                newsParameterList.Add(newValmodel);
            }
            news.Sumary = HttpUtility.HtmlDecode(news.Sumary);
            var model = new NewsViewModel
            {
                DatePublished = news.DatePublished,
                CreateAt = news.CreateAt,
                CreateBy = news.CreateBy,
                ID = news.ID,
                IsPublished = news.IsPublished,
                Link = news.Link,
                MediaID = news.MediaID,
                MediaName = news.MediaName,
                NewsCategory = news.NewsCategory,
                Sumary = news.Sumary.IndentHtml(),
                Title = news.Title,
                Media = new SelectList(media, "ID", "Name"),
                ListNewsCategory = listNC,
                ListNewsValuesModel = newsParameterList
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(NewsViewModel model)
        {
            if (ModelState.IsValid)
            {
                NewsCategory newsCategory = NewsCategoryBOL.GetNewsCategoryById(model.NewsCategory);
                News news = new News();
                Int64 newsID = model.ID;
                news.ID = model.ID;
                news.CreateAt = model.CreateAt;
                news.CreateBy = model.CreateBy;
                news.NewsCategory = model.NewsCategory;
                news.NewsCategoryName = newsCategory.Name;
                news.MediaID = model.MediaID;
                news.MediaName = model.MediaName;
                news.DatePublished = model.DatePublished;
                news.Title = model.Title;
                news.Sumary = model.Sumary;
                news.Link = model.Link;
                string btnSubmit = Request["btnSubmit"];
                switch (btnSubmit)
                {
                    case "Save":
                        news.IsPublished = false;
                        break;
                    case "Save & Published":
                        news.IsPublished = true;
                        break;
                }
                NewsBOL.Update(news);
                // Add Value
                if (newsID > 0)
                {
                    foreach (NewsValuesModel _n in model.ListNewsValuesModel)
                    {
                        NewsValues nVal = new NewsValues();
                        nVal.ID = _n.ID;
                        nVal.NewsID = newsID;
                        nVal.NewsParameterID = _n.NewsParameterID;
                        nVal.CreateAt = _n.CreateAt;
                        nVal.CreateBy = _n.CreateBy;
                        IRConstants.ChooseNews x = CommonFunctions.StringToEnum<IRConstants.ChooseNews>(_n.NewsParameterValue);
                        switch (x)
                        {
                            case IRConstants.ChooseNews.No:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.No;
                                break;
                            case IRConstants.ChooseNews.NotSure:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.NotSure;
                                break;
                            case IRConstants.ChooseNews.Yes:
                                nVal.NewsParameterValue = (byte)IRConstants.ChooseNews.Yes;
                                break;
                            default:
                                break;
                        }
                        IRConstants.NewsMark newsMark = CommonFunctions.StringToEnum<IRConstants.NewsMark>(_n.NewsMarkString);
                        switch (newsMark)
                        {
                            case IRConstants.NewsMark.one:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.one;
                                break;
                            case IRConstants.NewsMark.two:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.two;
                                break;
                            case IRConstants.NewsMark.three:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.three;
                                break;
                            case IRConstants.NewsMark.four:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.four;
                                break;
                            case IRConstants.NewsMark.five:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.five;
                                break;
                            case IRConstants.NewsMark.six:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.six;
                                break;
                            case IRConstants.NewsMark.seven:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.seven;
                                break;
                            case IRConstants.NewsMark.eight:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.eight;
                                break;
                            case IRConstants.NewsMark.nine:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.nine;
                                break;
                            case IRConstants.NewsMark.ten:
                                nVal.NewsMark = (byte)IRConstants.NewsMark.ten;
                                break;
                            default:
                                break;
                        }
                        NewsValuesBOL.Update(nVal);
                    }
                }
            }
            else
            {
                return View(model);
            }
            return RedirectToAction("Index");
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult _DeleteAjaxEditing(int id)
        {
            //Find a customer with ProductID equal to the id action parameter            
            if (id > 0)
            {
                //Delete the record
                if (!NewsBOL.Delete(id))
                {
                    ModelState.AddModelError(String.Empty, @"Have some errors. Please contact website's administrator!");
                }
            }
            //Rebind the grid
            return View(new GridModel(NewsBOL.GetAllNewsJoin()));
        }

        public JsonResult GetMediaCategoryNameAjax(int? mediaID)
        {
            return Json(MediaCategoryBOL.GetNameByMediaID(mediaID ?? 0));
        }
    }
}
